package com.bangdream.michelia.tool.retrofithttp.picasso;

import android.content.Context;
import android.support.annotation.NonNull;

import com.bangdream.michelia.utils.MyLog;
import com.bangdream.michelia.utils.NetworkUtil;

import java.io.IOException;

import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/1/11.
 * 拦截器Interceptor
 * 拦截器大家都不陌生，尤其是玩过okhttp和retofit的朋友，那肯定是拦截http的拦截请求和响应的
 */

public class CaheInterceptor implements Interceptor {

	private Context context;

	public CaheInterceptor(@NonNull Context context) {
		this.context = context;
	}

	@Override
	public Response intercept(Chain chain) throws IOException {
		Request request = chain.request();
		if (NetworkUtil.isConnected(context)) {
			Response response = chain.proceed(request);
			// read from cache for 60 s
			int maxAge = 300;
			String cacheControl = request.cacheControl().toString();
			MyLog.e("Tamic", maxAge + "s load cahe:" + cacheControl);
			return response.newBuilder()
					.removeHeader("Pragma")
					.removeHeader("Cache-Control")
					.header("Cache-Control", "public, max-age=" + maxAge)
					.build();
		} else {
			MyLog.e("Tamic", " no network load cahe");
			request = request.newBuilder()
					.cacheControl(CacheControl.FORCE_CACHE)
					.build();
			Response response = chain.proceed(request);
			//set cahe times is 3 days
			int maxStale = 60 * 60 * 24 * 3;
			return response.newBuilder()
					.removeHeader("Pragma")
					.removeHeader("Cache-Control")
					.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
					.build();
		}
	}
}